import axios from 'axios'

export const getCity = () => {
  const localCity = JSON.parse(localStorage.getItem('hkzf_city'))

  if(!localCity) {
    return new Promise((resolve, reject) => {
      const myCity = new window.BMap.LocalCity()
      myCity.get(async result => {
        const cityName = result.name 
        try {
          const {data} = await axios.get(`http://127.0.0.1:8080/area/info?name=${cityName}`)
          localStorage.setItem('hkzf_city',  JSON.stringify(data.body) )

          resolve(data.body)
        } catch(err) {
          reject(err)
        }
      })
     
    })
  }

  return Promise.resolve(localCity)
}